// pages/doctor/add/add.js
const jsonData = require("../jsondata.js");
var docotrservice = require("../../../apiservice/doctor/doctorservice");
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		doctorItem: {
			Lastname: '',
			MobilePhone: '',
			New_age: null,
			New_wechat: null
		},
		checkboxResult: ['白内障'],
		//选项集
		officeArray: jsonData.officeArray,
		officeIndex: 0,
    typeArray:jsonData.typeArray,
    typeIndex:0,
		titleArray: jsonData.titleArray,
		titleIndex: 0,
		jobArray: jsonData.jobArray,
		jobIndex: 0,
		genderArray: jsonData.genderArray,
		operationType: jsonData.operationType,
		genderIndex: 0,
		intention: [{
			id: 0,
			name: '无'
		}, {
			id: 1,
			name: '有'
		}], //合作意向
		intentionIndex: 0,
		hospitalName: '请选择',
		hospitalId: '',
		selectType: [],
	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function(options) {

	},

	onChange(ev) {
		this.setData({
			checkboxResult: ev.detail
		})
	},
	bindIntentionChange: function(e) {
		this.setData({
			intentionIndex: e.detail.value
		});
	},
	/**
	 * 文本框赋值
	 * @param {} e 
	 */
	onNameChange: function(e) {
		//console.log("onNameChange",e);
		this.setData({
			['doctorItem.Lastname']: e.detail
		})
	},
	onMobileChange: function(e) {
		//console.log("onMobileChange ",e);
		this.setData({
			['doctorItem.MobilePhone']: e.detail
		})
	},
	onAgeChange: function(e) {
		//console.log("onAgeChange",e);
		this.setData({
			['doctorItem.New_age']: e.detail
		})
	},
	onWeChatChange: function(e) {
		//console.log("onWeChatChange",e);
		this.setData({
			['doctorItem.New_wechat']: e.detail
		})
	},
	/**
	 * 选项集赋值
	 * @param {*} e 
	 */
	bindOfficeChange: function(e) {
		console.log("bindOfficeChange", e);
		this.setData({
			officeIndex: e.detail.value
		})
	},
  bindTypeChange: function (e) {
    this.setData({
      typeIndex: e.detail.value
    })
  },
	bindTitleChange: function(e) {
		//console.log("bindTitleChange",e);
		this.setData({
			titleIndex: e.detail.value
		})
	},
	bindJobChange: function(e) {
		//console.log("bindJobChange",e);
		this.setData({
			jobIndex: e.detail.value
		})
	},
	bindGenderChange: function(e) {
		//console.log("bindGenderChange",e);
		this.setData({
			genderIndex: e.detail.value
		})
	},
	onSelect: function(e) {


		console.log(e);
		this.setData({
			selectType: e.detail
		});
	},
	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function() {

	},
	toSelectHospital: function() {
		var that = this;
		wx.navigateTo({
			url: "../../institution/list/list",
			events: {
				openedHospitalPage: function(e) {
					//console.log(e.data)
				},
				passHospitalId:function(e){
					
				},
				HospitalEvent: function(data) {
					console.log(data)
					that.setData({
						hospitalId: data.competitorId,
						hospitalName: data.competitorName
					});
				}
			},
			success: function(res) {
				res.eventChannel.emit('openedInstitutionPage', {
					data: 'choose'
				})
				res.eventChannel.emit('passHospitalId', {
					data: that.data.hospitalId
				})
			}
		})
	},
	//新增医生
	addDoctor: function() {
		var doctorobj = this.getDoctorObj();
		var msg = '';
		var isCreate = true;
		var flag = true;
    //if (doctorobj.New_type == -1 && flag == true) {
    if (this.data.typeArray[this.data.typeIndex].name=="请选择"  && flag==true){
      msg = '医生类型不能为空';
      isCreate = false;
      flag = false;
    }

		if (doctorobj.Lastname.replace(/\s+/g, '') == '' && flag == true) {
			msg = '姓名不能为空';
			isCreate = false;
			flag = false;
		}
		if (doctorobj.GenderCode == 0 && flag == true) {
			msg = '性别不能为空';
			isCreate = false;
			flag = false;
		}
		if (doctorobj.New_age != '') {
			if (doctorobj.New_age > 150) {
				msg = '请填写正确的年龄';
				isCreate = false;
				flag = false;
			}
		}
		
		if (flag) {
			if (doctorobj.MobilePhone.replace(/\s+/g, '') == '') {
				msg = '手机不能为空';
				isCreate = false;
				flag = false;
			} else {
				if (!(/^1[3456789]\d{9}$/.test(doctorobj.MobilePhone))) {
					msg = '手机号码不正确';
					isCreate = false;
					flag = false;
				}
			}
		}
		if (doctorobj.New_hospitalname == '' && flag == true) {
			msg = '医疗机构名称不能为空';
			isCreate = false;
			flag = false;
		}
    if (this.data.officeIndex == 0 && flag == true) {
      msg = '科室不能为空';
      isCreate = false;
      flag = false;
    }


		if (isCreate == false) {
			wx.showToast({
				title: msg,
				icon: 'none',
				duration: 2000
			})
		} else {
			console.log(doctorobj);
			this.AddDoctor(doctorobj);
		}

	},
	getDoctorObj: function() {
		var doctorobj = this.data.doctorItem;
		doctorobj.New_type = this.data.typeArray[this.data.typeIndex].id; //联系人类型
		doctorobj.GenderCode = this.data.genderIndex //性别
		doctorobj.New_hospitalname = this.data.hospitalId; //医院
		if (this.data.officeIndex != 0) {
			doctorobj.New_administrativeortechnicaloffices = this.data.officeArray[this.data.officeIndex].id; //科室
		}
		if (this.data.titleIndex != 0) {
			doctorobj.New_title = this.data.titleArray[this.data.titleIndex].id; //职称
		}
		if (this.data.jobIndex != 0) {
			doctorobj.New_hospitalduty = this.data.jobArray[this.data.jobIndex].id; //职务
			doctorobj.JobTitle = this.data.jobArray[this.data.jobIndex].name;
		}
		doctorobj.New_cataract = false;
		doctorobj.New_strabismus = false;
		doctorobj.New_glaucoma = false;
		doctorobj.New_lacrimalpassage = false;
		doctorobj.New_ametropia = false;
		doctorobj.New_fundustangent = false;
		var intention = false;
		if (this.data.intentionIndex == 1) {
			intention = true;
		}
		doctorobj.New_cooperativeintention = intention; //合作意向
		var selectType = this.data.selectType;
		if (selectType.length > 0) {
			for (var i = 0; i < selectType.length; i++) {
				if (selectType[i] == 1) {
					doctorobj.New_cataract = true;
				} else if (selectType[i] == 2) {
					doctorobj.New_strabismus = true;
				} else if (selectType[i] == 3) {
					doctorobj.New_glaucoma = true;
				} else if (selectType[i] == 4) {
					doctorobj.New_lacrimalpassage = true;
				} else if (selectType[i] == 5) {
					doctorobj.New_ametropia = true;
				} else if (selectType[i] == 6) {
					doctorobj.New_fundustangent = true;
				}
			}
		}
		return doctorobj
	},
	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function() {

	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function() {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function() {

	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: function() {

	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function() {

	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage: function() {

	},
	// 新增医生
	AddDoctor: function(obj) {

		var that = this;
		var msg = '创建';
    wx.showToast({
      title: '提交中...',
      icon:"loading",
      mask:true
    })
		docotrservice.AddDoctor(obj).then(function(success) {
			that.showResult(success.data, msg);
			that.setData({
				doctorItem: {
					Lastname: '',
					MobilePhone: '',
					New_age: null,
					New_wechat: null
				},
				genderIndex: 0,
				officeIndex: 0,
        typeIndex:0,
				titleIndex: 0,
				jobIndex: 0,
				intentionIndex: 0,
				hospitalName: '请选择',
				hospitalId: '',
				selectType: []
			})

			let optypechecklist = that.selectComponent("#optypechecklist");
			if (optypechecklist)
				optypechecklist.clearSelectItem();

		}).catch(function(ex) {
			console.log(ex);
		})
	},
	showResult: function(res, msg) {
		var title = '';
		var icon = '';
		if (res.resultState == 1) {
			title = msg + '成功';
			icon = 'success';
		} else {
			title = msg + '失败';
			icon = 'none';
		}
		wx.showToast({
			title: title,
			icon: icon,
			duration: 1500
		});
	},
})
